Software Tools for Earth and Environmental Science

– 9th WEEK –

Emir Toker

14/11/2019

R Promgramming - Part 1

  • Syllabus and Book
  • DataCamp Class
  • Repeat - R Language
  • Practice - R Language
  • Practice - Create a Function

Coffee Break

  • R Programming - Conditions
    • Comparison Operators
    • if Statement
    • else Statement
    • ifelse Statement
  • Next Week

(Optional) Practice - MidTerm Project

Syllabus and Book

Syllabus

Extended Syllabus PDF

Book

PDF - (179 - 185)

DataCamp - Class

DataCamp

LINK

DataCamp

My Class

DataCamp

Home - My Class

DataCamp

Team - Introduction to R

DataCamp

Team Assignments !!

DataCamp

1000 XP - 1hour/Week

Related Chapters and Courses

DataCamp

Each +100XP

Take Hint -30 XP

DataCamp

Introduction to R Course

4 hours

62 Excersize

6200 XP

Also available on mobile

DataCamp

Search - “R

DataCamp

DataCamp

Only “Intro” is Free

DataCamp

But you can use all

DataCamp

If you not a member of team

DataCamp

You have to pay

DataCamp

(1$ = 5,75 TL)

29$ * 5,75 TL * 6 Months * 1 Student= 833,75 TL

833,75 TL * 25 Students = 20.843,75 TL

R Language - Repeat

R Language - Repeat

  • Create/Open R Project or R File
  • Basic Math, Assigment, Comment
  • Data Types - Classes
    • Numeric
    • Integer
    • Logical
    • Character
  • Data Structures - Objects
    • Vector
    • Matrice
    • Array
    • Data Frame
    • List
  • Special Values, Attributes

Create/Open R Project or R File

Project

File - New Project - New Directory

Project

New Project

Project

Directory Name - Create Project

NEW Project is Ready

File

File - New File - Script

Basic Math, Assigment, Comment

Basic Math, Assigment, Comment

  • Arithmetic Operators ( + - * / ^ )

  • Scientific Math

  • Assignment Arrow (<-)

a <- 3.8
  • Comment Lines, Comment Out, Hash (#)
# value <- old_code()
value <- new_code()

Data Types - Classes

Data Types - Classes

  • Numeric
  • Integer
  • Logical
  • Character
  • Complex
  • Date
a <- 4
b <- 3:9
c <- 7L
d <- 1i
e <- 5 < -9
f <- "23"

date <- "2007-06-22"
date1 <- as.Date(date)
date2 <- as.Date("2004-02-13")

date1 - date2
## Time difference of 1225 days

Coercion

Data Structures - Objects

Data Structures - Objects

Data Structures - Vector

c()

spring_months <- c("March", "April", "May", "June")
spring_months[2] <- "new"
spring_months
## [1] "March" "new"   "May"   "June"
myvec1 <- c(1, 3, 1, 42)
a <- 35
myvec2 <- c(3L, myvec1, 1e+03, 64^0.5, 2+(3-1.1)/9.44, a)
myvec2
## [1]    3.000000    1.000000    3.000000    1.000000   42.000000 1000.000000
## [7]    8.000000    2.201271   35.000000

?lenght ?seq ?round ?rep ?sort ?runif ?set.seed

Data Structures - Matrice

matrix(data = ,nrow = ,ncol = )

data <- runif(9,1,100)    
data
## [1] 12.915285 93.166266 91.116730 30.696196 11.418564 98.591149  3.229152
## [8] 95.188369 52.501424
matrix <- matrix(data, nrow = 3, ncol = 3)
matrix
##          [,1]     [,2]      [,3]
## [1,] 12.91528 30.69620  3.229152
## [2,] 93.16627 11.41856 95.188369
## [3,] 91.11673 98.59115 52.501424
print(c(length(matrix),dim(matrix)))
## [1] 9 3 3

Data Structures - Array

array(data = ,dim = )

data <- 1:24
array <- array(data, dim = c(4,3,2))    # raw, col, level
array
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   13   17   21
## [2,]   14   18   22
## [3,]   15   19   23
## [4,]   16   20   24
array[2,2,1]    # raw, col, level
## [1] 6

Data Structures - Factor

gender = c("male", "female", "male", "male", "female")
gender
## [1] "male"   "female" "male"   "male"   "female"
class(gender)
## [1] "character"
str(gender)
##  chr [1:5] "male" "female" "male" "male" "female"
gender[2]
## [1] "female"

Data Structures - Factor

gender_factor <- factor(c("male", "female", "male", "male", "female"))
gender_factor
## [1] male   female male   male   female
## Levels: female male
class(gender_factor)
## [1] "factor"
str(gender_factor)
##  Factor w/ 2 levels "female","male": 2 1 2 2 1
gender_factor[2]
## [1] female
## Levels: female male

Data Structures - Data Frame

data.frame(data1,data2,data3…)

person=c("Peter", "Lois", "Meg", "Chris", "Stewie")
age=c(42, 40, 17, 14 ,1)
sex=factor(c("M", "F", "F", "M", "M"))
married=c(TRUE, TRUE, FALSE, FALSE, FALSE)
df <- data.frame(person, age, sex, married)
df
##   person age sex married
## 1  Peter  42   M    TRUE
## 2   Lois  40   F    TRUE
## 3    Meg  17   F   FALSE
## 4  Chris  14   M   FALSE
## 5 Stewie   1   M   FALSE
str(df)
## 'data.frame':    5 obs. of  4 variables:
##  $ person : Factor w/ 5 levels "Chris","Lois",..: 4 2 3 1 5
##  $ age    : num  42 40 17 14 1
##  $ sex    : Factor w/ 2 levels "F","M": 2 1 1 2 2
##  $ married: logi  TRUE TRUE FALSE FALSE FALSE

Data Structures - Data Frame

data.frame(data1,data2,data3…)

person=c("Peter", "Lois", "Meg", "Chris", "Stewie")
age=c(42, 40, 17, 14 ,1)
sex=factor(c("M", "F", "F", "M", "M"))
married=c(TRUE, TRUE, FALSE, FALSE, FALSE)
df <- data.frame(person ,age, sex, married, stringsAsFactors=FALSE)
df
##   person age sex married
## 1  Peter  42   M    TRUE
## 2   Lois  40   F    TRUE
## 3    Meg  17   F   FALSE
## 4  Chris  14   M   FALSE
## 5 Stewie   1   M   FALSE
str(df)
## 'data.frame':    5 obs. of  4 variables:
##  $ person : chr  "Peter" "Lois" "Meg" "Chris" ...
##  $ age    : num  42 40 17 14 1
##  $ sex    : Factor w/ 2 levels "F","M": 2 1 1 2 2
##  $ married: logi  TRUE TRUE FALSE FALSE FALSE

Data Structures - Data Frame

data.frame(data1,data2,data3…)

df[1]
##   person
## 1  Peter
## 2   Lois
## 3    Meg
## 4  Chris
## 5 Stewie
df[[1]]       # df$person
## [1] "Peter"  "Lois"   "Meg"    "Chris"  "Stewie"
df[[1]][1]
## [1] "Peter"

Data Structures - List

list(data1,data2,data3…)

matrix <- matrix(data=1:4,nrow=2,ncol=2)
vector <- c(T,F,T,T)
var <- "hello"
data_frame <- data.frame(person ,age, sex, married, stringsAsFactors=FALSE)
list  <- list(matrix,vector,var,data_frame)
list
## [[1]]
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## [[2]]
## [1]  TRUE FALSE  TRUE  TRUE
## 
## [[3]]
## [1] "hello"
## 
## [[4]]
##   person age sex married
## 1  Peter  42   M    TRUE
## 2   Lois  40   F    TRUE
## 3    Meg  17   F   FALSE
## 4  Chris  14   M   FALSE
## 5 Stewie   1   M   FALSE

Special Values, Attributes

Special Values

NA, NaN, NULL, Inf

class(NA)     # Not Available (“missing” entity)
## [1] "logical"
class(NaN)    # Not a Number
## [1] "numeric"
class(NULL)   # Null (“empty” entity)
## [1] "NULL"
class(Inf)     # Infinity
## [1] "numeric"

Attributes

person=c("Peter", "Lois", "Meg", "Chris", "Stewie")
age=c(42, 40, 17, 14 ,1)
sex=factor(c("M", "F", "F", "M", "M"))
married=c(TRUE, TRUE, FALSE, FALSE, FALSE)

data_frame <- data.frame(person ,age, sex, married, stringsAsFactors=FALSE)
data_frame
##   person age sex married
## 1  Peter  42   M    TRUE
## 2   Lois  40   F    TRUE
## 3    Meg  17   F   FALSE
## 4  Chris  14   M   FALSE
## 5 Stewie   1   M   FALSE
data_frame[2]
##   age
## 1  42
## 2  40
## 3  17
## 4  14
## 5   1

Attributes

attributes(data_frame)
## $names
## [1] "person"  "age"     "sex"     "married"
## 
## $class
## [1] "data.frame"
## 
## $row.names
## [1] 1 2 3 4 5
attr(data_frame,"row.names") <- c("bir", "iki", "uc", "dort","bes")

data_frame
##      person age sex married
## bir   Peter  42   M    TRUE
## iki    Lois  40   F    TRUE
## uc      Meg  17   F   FALSE
## dort  Chris  14   M   FALSE
## bes  Stewie   1   M   FALSE

Practice - R Language

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
mydata <- read.csv(file = "18397_Cekmekoy_Omerli_15dk.txt", 
                   header = TRUE, 
                   sep = ";")
mydata
##     sta_no year month day hour minutes temp precipitation pressure
## 1    18397 2017     7  26   18       0 23.9          0.00   1003.0
## 2    18397 2017     7  26   18      15 23.9          0.00   1003.1
## 3    18397 2017     7  26   18      30 23.8          0.00   1003.2
## 4    18397 2017     7  26   18      45 23.8          0.00   1003.2
## 5    18397 2017     7  26   19       0 23.6          0.00   1003.2
## 6    18397 2017     7  26   19      15 23.2          0.00   1003.1
## 7    18397 2017     7  26   19      30 23.2          0.00   1003.1
## 8    18397 2017     7  26   19      45 23.1          0.00   1003.1
## 9    18397 2017     7  26   20       0 23.0          0.00   1003.1
## 10   18397 2017     7  26   20      15 22.8          0.00   1003.0
## 11   18397 2017     7  26   20      30 22.5          0.00   1003.0
## 12   18397 2017     7  26   20      45 22.4          0.00   1003.0
## 13   18397 2017     7  26   21       0 22.2          0.00   1003.0
## 14   18397 2017     7  26   21      15 22.3          0.00   1003.0
## 15   18397 2017     7  26   21      30 22.2          0.00   1003.1
## 16   18397 2017     7  26   21      45 21.7          0.00   1003.1
## 17   18397 2017     7  26   22       0 21.9          0.00   1003.2
## 18   18397 2017     7  26   22      15 21.7          0.00   1003.3
## 19   18397 2017     7  26   22      30 21.6          0.00   1003.3
## 20   18397 2017     7  26   22      45 22.2          0.00   1003.4
## 21   18397 2017     7  26   23       0 22.2          0.00   1003.4
## 22   18397 2017     7  26   23      15 22.1          0.00   1003.5
## 23   18397 2017     7  26   23      30 22.3          0.00   1003.4
## 24   18397 2017     7  26   23      45 22.5          0.00   1003.4
## 25   18397 2017     7  27    0       0 22.3          0.00   1003.4
## 26   18397 2017     7  27    0      15 22.2          0.00   1003.2
## 27   18397 2017     7  27    0      30 22.5          0.00   1003.2
## 28   18397 2017     7  27    0      45 22.6          0.00   1003.2
## 29   18397 2017     7  27    1       0 22.6          0.00   1003.3
## 30   18397 2017     7  27    1      15 22.6          0.00   1003.4
## 31   18397 2017     7  27    1      30 22.6          0.00   1003.2
## 32   18397 2017     7  27    1      45 22.7          0.00   1003.2
## 33   18397 2017     7  27    2       0 22.6          0.00   1003.3
## 34   18397 2017     7  27    2      15 22.5          0.00   1003.2
## 35   18397 2017     7  27    2      30 22.6          0.00   1003.2
## 36   18397 2017     7  27    2      45 22.5          0.00   1003.1
## 37   18397 2017     7  27    3       0 22.5          0.00   1003.1
## 38   18397 2017     7  27    3      15 22.4          0.00   1003.0
## 39   18397 2017     7  27    3      30 22.5          0.00   1003.1
## 40   18397 2017     7  27    3      45 22.4          0.00   1003.3
## 41   18397 2017     7  27    4       0 22.5          0.00   1003.4
## 42   18397 2017     7  27    4      15 22.6          0.00   1003.5
## 43   18397 2017     7  27    4      30 23.0          0.00   1003.5
## 44   18397 2017     7  27    4      45 23.2          0.00   1003.5
## 45   18397 2017     7  27    5       0 24.2          0.00   1003.6
## 46   18397 2017     7  27    5      15 25.1          0.00   1003.5
## 47   18397 2017     7  27    5      30 25.5          0.00   1003.4
## 48   18397 2017     7  27    5      45 26.1          0.00   1003.3
## 49   18397 2017     7  27    6       0 27.1          0.00   1003.3
## 50   18397 2017     7  27    6      15 26.9          0.00   1003.3
## 51   18397 2017     7  27    6      30 27.6          0.00   1003.3
## 52   18397 2017     7  27    6      45 28.0          0.00   1003.2
## 53   18397 2017     7  27    7       0 28.4          0.00   1003.1
## 54   18397 2017     7  27    7      15 28.5          0.00   1003.1
## 55   18397 2017     7  27    7      30 29.3          0.00   1003.0
## 56   18397 2017     7  27    7      45 30.2          0.00   1002.9
## 57   18397 2017     7  27    8       0 30.1          0.00   1002.8
## 58   18397 2017     7  27    8      15 30.1          0.00   1002.8
## 59   18397 2017     7  27    8      30 30.4          0.00   1002.8
## 60   18397 2017     7  27    8      45 30.4          0.00   1002.8
## 61   18397 2017     7  27    9       0 30.8          0.00   1002.9
## 62   18397 2017     7  27    9      15 30.9          0.00   1002.8
## 63   18397 2017     7  27    9      30 31.0          0.00   1002.6
## 64   18397 2017     7  27    9      45 31.5          0.00   1002.6
## 65   18397 2017     7  27   10       0 31.2          0.00   1002.6
## 66   18397 2017     7  27   10      15 30.9          0.00   1002.4
## 67   18397 2017     7  27   10      30 30.9          0.00   1002.4
## 68   18397 2017     7  27   10      45 30.4          0.00   1002.3
## 69   18397 2017     7  27   11       0 30.4          0.00   1002.1
## 70   18397 2017     7  27   11      15 30.0          0.00   1001.9
## 71   18397 2017     7  27   11      30 29.2          0.00   1001.9
## 72   18397 2017     7  27   11      45 29.5          0.00   1001.7
## 73   18397 2017     7  27   12       0 29.4          0.00   1001.6
## 74   18397 2017     7  27   12      15 29.3          0.00   1001.3
## 75   18397 2017     7  27   12      30 29.6          0.00   1001.2
## 76   18397 2017     7  27   12      45 28.8          0.00   1001.3
## 77   18397 2017     7  27   13       0 29.0          0.00   1001.1
## 78   18397 2017     7  27   13      15 29.0          0.00   1001.2
## 79   18397 2017     7  27   13      30 29.2          0.00   1001.3
## 80   18397 2017     7  27   13      45 28.4          0.00   1001.5
## 81   18397 2017     7  27   14       0 27.8          0.00   1001.6
## 82   18397 2017     7  27   14      15 27.4          0.00   1001.6
## 83   18397 2017     7  27   14      30 26.6          0.00   1001.5
## 84   18397 2017     7  27   14      45 26.2          0.00   1001.2
## 85   18397 2017     7  27   15       0 25.8          0.00   1001.1
## 86   18397 2017     7  27   15      15 25.6          0.00   1001.0
## 87   18397 2017     7  27   15      30 25.4          0.00   1000.9
## 88   18397 2017     7  27   15      45 24.2          0.00   1001.8
## 89   18397 2017     7  27   16       0 19.2          7.01   1003.7
## 90   18397 2017     7  27   16      15 19.5         15.81   1003.2
## 91   18397 2017     7  27   16      30 20.1         16.06   1003.1
## 92   18397 2017     7  27   16      45 20.8         16.06   1003.7
## 93   18397 2017     7  27   17       0 21.2         17.19  -9999.0
## 94   18397 2017     7  27   17      15 21.4         17.21   1005.6
## 95   18397 2017     7  27   17      30 21.4         18.46   1005.4
## 96   18397 2017     7  27   17      45 21.4         21.21   1005.1
## 97   18397 2017     7  27   18       0 21.2         21.21   1005.1
## 98   18397 2017     7  27   18      15 21.0         21.21  -9999.0
## 99   18397 2017     7  27   18      30 20.8         21.21   1006.3
## 100  18397 2017     7  27   18      45 20.9         21.21  -9999.0
## 101  18397 2017     7  27   19       0 20.8         21.40   1005.7
## 102  18397 2017     7  27   19      15 20.7         21.40   1006.2
## 103  18397 2017     7  27   19      30 20.8         21.60   1003.6
## 104  18397 2017     7  27   19      45 20.8         21.82   1003.7
## 105  18397 2017     7  27   20       0 20.9         21.82  -9999.0
## 106  18397 2017     7  27   20      15 20.6         21.82  -9999.0
## 107  18397 2017     7  27   20      30 20.6         21.82   1005.1
## 108  18397 2017     7  27   20      45 20.5         21.82   1005.6
## 109  18397 2017     7  27   21       0 20.7         21.82   1005.5
## 110  18397 2017     7  27   21      15 20.8         21.82   1005.7
## 111  18397 2017     7  27   21      30 20.4         21.82   1005.6
## 112  18397 2017     7  27   21      45 20.4         21.82   1005.8
## 113  18397 2017     7  27   22       0 20.6         21.82   1005.8
## 114  18397 2017     7  27   22      15 20.5         21.82   1005.9
## 115  18397 2017     7  27   22      30 20.4         21.82   1006.0
## 116  18397 2017     7  27   22      45 20.5         21.82   1005.9
## 117  18397 2017     7  27   23       0 20.5         21.82   1005.9
## 118  18397 2017     7  27   23      15 20.6         21.82   1005.9
## 119  18397 2017     7  27   23      30 20.5         21.82   1006.0
## 120  18397 2017     7  27   23      45 20.5         21.82   1006.0
## 121  18397 2017     7  28    0       0 20.4         21.82   1006.0
##     relative_humidity
## 1                  94
## 2                  95
## 3                  96
## 4                  96
## 5                  96
## 6                  97
## 7                  97
## 8                  98
## 9                  98
## 10                 98
## 11                 98
## 12                 99
## 13                 99
## 14                 99
## 15                 99
## 16                 99
## 17                 99
## 18                 99
## 19                 99
## 20                100
## 21                100
## 22                100
## 23                100
## 24                100
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Check the class and structure of your new data.
## [1] "data.frame"
## 'data.frame':    121 obs. of  10 variables:
##  $ sta_no           : int  18397 18397 18397 18397 18397 18397 18397 18397 18397 18397 ...
##  $ year             : int  2017 2017 2017 2017 2017 2017 2017 2017 2017 2017 ...
##  $ month            : int  7 7 7 7 7 7 7 7 7 7 ...
##  $ day              : int  26 26 26 26 26 26 26 26 26 26 ...
##  $ hour             : int  18 18 18 18 19 19 19 19 20 20 ...
##  $ minutes          : int  0 15 30 45 0 15 30 45 0 15 ...
##  $ temp             : num  23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23 22.8 ...
##  $ precipitation    : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ pressure         : num  1003 1003 1003 1003 1003 ...
##  $ relative_humidity: int  94 95 96 96 96 97 97 98 98 98 ...
## $names
##  [1] "sta_no"            "year"              "month"            
##  [4] "day"               "hour"              "minutes"          
##  [7] "temp"              "precipitation"     "pressure"         
## [10] "relative_humidity"
## 
## $class
## [1] "data.frame"
## 
## $row.names
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102
## [103] 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
## [120] 120 121

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Take the “Temperature” parameter and assign it as a new variable.
temp_data <- mydata$temp
temp_data
##   [1] 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23.0 22.8 22.5 22.4 22.2 22.3
##  [15] 22.2 21.7 21.9 21.7 21.6 22.2 22.2 22.1 22.3 22.5 22.3 22.2 22.5 22.6
##  [29] 22.6 22.6 22.6 22.7 22.6 22.5 22.6 22.5 22.5 22.4 22.5 22.4 22.5 22.6
##  [43] 23.0 23.2 24.2 25.1 25.5 26.1 27.1 26.9 27.6 28.0 28.4 28.5 29.3 30.2
##  [57] 30.1 30.1 30.4 30.4 30.8 30.9 31.0 31.5 31.2 30.9 30.9 30.4 30.4 30.0
##  [71] 29.2 29.5 29.4 29.3 29.6 28.8 29.0 29.0 29.2 28.4 27.8 27.4 26.6 26.2
##  [85] 25.8 25.6 25.4 24.2 19.2 19.5 20.1 20.8 21.2 21.4 21.4 21.4 21.2 21.0
##  [99] 20.8 20.9 20.8 20.7 20.8 20.8 20.9 20.6 20.6 20.5 20.7 20.8 20.4 20.4
## [113] 20.6 20.5 20.4 20.5 20.5 20.6 20.5 20.5 20.4

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Plot the “temperature” vector.

    plot(temp_data)

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Print minimum temperature and find which element is the minimum in temperature vector.
print(min(temp_data))
## [1] 19.2
which(temp_data==min(temp_data))
## [1] 89
which(temp_data==19.2)
## [1] 89

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. change the minimum value with NA and Print.
temp_data[which(temp_data==min(temp_data))] <- NA

temp_data[which(temp_data==19.2)] <- NA

temp_data[89] <- NA

print(temp_data)
##   [1] 23.9 23.9 23.8 23.8 23.6 23.2 23.2 23.1 23.0 22.8 22.5 22.4 22.2 22.3
##  [15] 22.2 21.7 21.9 21.7 21.6 22.2 22.2 22.1 22.3 22.5 22.3 22.2 22.5 22.6
##  [29] 22.6 22.6 22.6 22.7 22.6 22.5 22.6 22.5 22.5 22.4 22.5 22.4 22.5 22.6
##  [43] 23.0 23.2 24.2 25.1 25.5 26.1 27.1 26.9 27.6 28.0 28.4 28.5 29.3 30.2
##  [57] 30.1 30.1 30.4 30.4 30.8 30.9 31.0 31.5 31.2 30.9 30.9 30.4 30.4 30.0
##  [71] 29.2 29.5 29.4 29.3 29.6 28.8 29.0 29.0 29.2 28.4 27.8 27.4 26.6 26.2
##  [85] 25.8 25.6 25.4 24.2   NA 19.5 20.1 20.8 21.2 21.4 21.4 21.4 21.2 21.0
##  [99] 20.8 20.9 20.8 20.7 20.8 20.8 20.9 20.6 20.6 20.5 20.7 20.8 20.4 20.4
## [113] 20.6 20.5 20.4 20.5 20.5 20.6 20.5 20.5 20.4

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Plot the new “temperature” vector.

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Replace these new temperature values with old temperature values located in your data frame.
mydata$temp <- temp_data

mydata
##     sta_no year month day hour minutes temp precipitation pressure
## 1    18397 2017     7  26   18       0 23.9          0.00   1003.0
## 2    18397 2017     7  26   18      15 23.9          0.00   1003.1
## 3    18397 2017     7  26   18      30 23.8          0.00   1003.2
## 4    18397 2017     7  26   18      45 23.8          0.00   1003.2
## 5    18397 2017     7  26   19       0 23.6          0.00   1003.2
## 6    18397 2017     7  26   19      15 23.2          0.00   1003.1
## 7    18397 2017     7  26   19      30 23.2          0.00   1003.1
## 8    18397 2017     7  26   19      45 23.1          0.00   1003.1
## 9    18397 2017     7  26   20       0 23.0          0.00   1003.1
## 10   18397 2017     7  26   20      15 22.8          0.00   1003.0
## 11   18397 2017     7  26   20      30 22.5          0.00   1003.0
## 12   18397 2017     7  26   20      45 22.4          0.00   1003.0
## 13   18397 2017     7  26   21       0 22.2          0.00   1003.0
## 14   18397 2017     7  26   21      15 22.3          0.00   1003.0
## 15   18397 2017     7  26   21      30 22.2          0.00   1003.1
## 16   18397 2017     7  26   21      45 21.7          0.00   1003.1
## 17   18397 2017     7  26   22       0 21.9          0.00   1003.2
## 18   18397 2017     7  26   22      15 21.7          0.00   1003.3
## 19   18397 2017     7  26   22      30 21.6          0.00   1003.3
## 20   18397 2017     7  26   22      45 22.2          0.00   1003.4
## 21   18397 2017     7  26   23       0 22.2          0.00   1003.4
## 22   18397 2017     7  26   23      15 22.1          0.00   1003.5
## 23   18397 2017     7  26   23      30 22.3          0.00   1003.4
## 24   18397 2017     7  26   23      45 22.5          0.00   1003.4
## 25   18397 2017     7  27    0       0 22.3          0.00   1003.4
## 26   18397 2017     7  27    0      15 22.2          0.00   1003.2
## 27   18397 2017     7  27    0      30 22.5          0.00   1003.2
## 28   18397 2017     7  27    0      45 22.6          0.00   1003.2
## 29   18397 2017     7  27    1       0 22.6          0.00   1003.3
## 30   18397 2017     7  27    1      15 22.6          0.00   1003.4
## 31   18397 2017     7  27    1      30 22.6          0.00   1003.2
## 32   18397 2017     7  27    1      45 22.7          0.00   1003.2
## 33   18397 2017     7  27    2       0 22.6          0.00   1003.3
## 34   18397 2017     7  27    2      15 22.5          0.00   1003.2
## 35   18397 2017     7  27    2      30 22.6          0.00   1003.2
## 36   18397 2017     7  27    2      45 22.5          0.00   1003.1
## 37   18397 2017     7  27    3       0 22.5          0.00   1003.1
## 38   18397 2017     7  27    3      15 22.4          0.00   1003.0
## 39   18397 2017     7  27    3      30 22.5          0.00   1003.1
## 40   18397 2017     7  27    3      45 22.4          0.00   1003.3
## 41   18397 2017     7  27    4       0 22.5          0.00   1003.4
## 42   18397 2017     7  27    4      15 22.6          0.00   1003.5
## 43   18397 2017     7  27    4      30 23.0          0.00   1003.5
## 44   18397 2017     7  27    4      45 23.2          0.00   1003.5
## 45   18397 2017     7  27    5       0 24.2          0.00   1003.6
## 46   18397 2017     7  27    5      15 25.1          0.00   1003.5
## 47   18397 2017     7  27    5      30 25.5          0.00   1003.4
## 48   18397 2017     7  27    5      45 26.1          0.00   1003.3
## 49   18397 2017     7  27    6       0 27.1          0.00   1003.3
## 50   18397 2017     7  27    6      15 26.9          0.00   1003.3
## 51   18397 2017     7  27    6      30 27.6          0.00   1003.3
## 52   18397 2017     7  27    6      45 28.0          0.00   1003.2
## 53   18397 2017     7  27    7       0 28.4          0.00   1003.1
## 54   18397 2017     7  27    7      15 28.5          0.00   1003.1
## 55   18397 2017     7  27    7      30 29.3          0.00   1003.0
## 56   18397 2017     7  27    7      45 30.2          0.00   1002.9
## 57   18397 2017     7  27    8       0 30.1          0.00   1002.8
## 58   18397 2017     7  27    8      15 30.1          0.00   1002.8
## 59   18397 2017     7  27    8      30 30.4          0.00   1002.8
## 60   18397 2017     7  27    8      45 30.4          0.00   1002.8
## 61   18397 2017     7  27    9       0 30.8          0.00   1002.9
## 62   18397 2017     7  27    9      15 30.9          0.00   1002.8
## 63   18397 2017     7  27    9      30 31.0          0.00   1002.6
## 64   18397 2017     7  27    9      45 31.5          0.00   1002.6
## 65   18397 2017     7  27   10       0 31.2          0.00   1002.6
## 66   18397 2017     7  27   10      15 30.9          0.00   1002.4
## 67   18397 2017     7  27   10      30 30.9          0.00   1002.4
## 68   18397 2017     7  27   10      45 30.4          0.00   1002.3
## 69   18397 2017     7  27   11       0 30.4          0.00   1002.1
## 70   18397 2017     7  27   11      15 30.0          0.00   1001.9
## 71   18397 2017     7  27   11      30 29.2          0.00   1001.9
## 72   18397 2017     7  27   11      45 29.5          0.00   1001.7
## 73   18397 2017     7  27   12       0 29.4          0.00   1001.6
## 74   18397 2017     7  27   12      15 29.3          0.00   1001.3
## 75   18397 2017     7  27   12      30 29.6          0.00   1001.2
## 76   18397 2017     7  27   12      45 28.8          0.00   1001.3
## 77   18397 2017     7  27   13       0 29.0          0.00   1001.1
## 78   18397 2017     7  27   13      15 29.0          0.00   1001.2
## 79   18397 2017     7  27   13      30 29.2          0.00   1001.3
## 80   18397 2017     7  27   13      45 28.4          0.00   1001.5
## 81   18397 2017     7  27   14       0 27.8          0.00   1001.6
## 82   18397 2017     7  27   14      15 27.4          0.00   1001.6
## 83   18397 2017     7  27   14      30 26.6          0.00   1001.5
## 84   18397 2017     7  27   14      45 26.2          0.00   1001.2
## 85   18397 2017     7  27   15       0 25.8          0.00   1001.1
## 86   18397 2017     7  27   15      15 25.6          0.00   1001.0
## 87   18397 2017     7  27   15      30 25.4          0.00   1000.9
## 88   18397 2017     7  27   15      45 24.2          0.00   1001.8
## 89   18397 2017     7  27   16       0   NA          7.01   1003.7
## 90   18397 2017     7  27   16      15 19.5         15.81   1003.2
## 91   18397 2017     7  27   16      30 20.1         16.06   1003.1
## 92   18397 2017     7  27   16      45 20.8         16.06   1003.7
## 93   18397 2017     7  27   17       0 21.2         17.19  -9999.0
## 94   18397 2017     7  27   17      15 21.4         17.21   1005.6
## 95   18397 2017     7  27   17      30 21.4         18.46   1005.4
## 96   18397 2017     7  27   17      45 21.4         21.21   1005.1
## 97   18397 2017     7  27   18       0 21.2         21.21   1005.1
## 98   18397 2017     7  27   18      15 21.0         21.21  -9999.0
## 99   18397 2017     7  27   18      30 20.8         21.21   1006.3
## 100  18397 2017     7  27   18      45 20.9         21.21  -9999.0
## 101  18397 2017     7  27   19       0 20.8         21.40   1005.7
## 102  18397 2017     7  27   19      15 20.7         21.40   1006.2
## 103  18397 2017     7  27   19      30 20.8         21.60   1003.6
## 104  18397 2017     7  27   19      45 20.8         21.82   1003.7
## 105  18397 2017     7  27   20       0 20.9         21.82  -9999.0
## 106  18397 2017     7  27   20      15 20.6         21.82  -9999.0
## 107  18397 2017     7  27   20      30 20.6         21.82   1005.1
## 108  18397 2017     7  27   20      45 20.5         21.82   1005.6
## 109  18397 2017     7  27   21       0 20.7         21.82   1005.5
## 110  18397 2017     7  27   21      15 20.8         21.82   1005.7
## 111  18397 2017     7  27   21      30 20.4         21.82   1005.6
## 112  18397 2017     7  27   21      45 20.4         21.82   1005.8
## 113  18397 2017     7  27   22       0 20.6         21.82   1005.8
## 114  18397 2017     7  27   22      15 20.5         21.82   1005.9
## 115  18397 2017     7  27   22      30 20.4         21.82   1006.0
## 116  18397 2017     7  27   22      45 20.5         21.82   1005.9
## 117  18397 2017     7  27   23       0 20.5         21.82   1005.9
## 118  18397 2017     7  27   23      15 20.6         21.82   1005.9
## 119  18397 2017     7  27   23      30 20.5         21.82   1006.0
## 120  18397 2017     7  27   23      45 20.5         21.82   1006.0
## 121  18397 2017     7  28    0       0 20.4         21.82   1006.0
##     relative_humidity
## 1                  94
## 2                  95
## 3                  96
## 4                  96
## 5                  96
## 6                  97
## 7                  97
## 8                  98
## 9                  98
## 10                 98
## 11                 98
## 12                 99
## 13                 99
## 14                 99
## 15                 99
## 16                 99
## 17                 99
## 18                 99
## 19                 99
## 20                100
## 21                100
## 22                100
## 23                100
## 24                100
## 25                100
## 26                100
## 27                100
## 28                100
## 29                100
## 30                100
## 31                100
## 32                100
## 33                100
## 34                100
## 35                100
## 36                100
## 37                100
## 38                100
## 39                100
## 40                100
## 41                100
## 42                100
## 43                100
## 44                100
## 45                100
## 46                 97
## 47                 84
## 48                 82
## 49                 79
## 50                 78
## 51                 78
## 52                 76
## 53                 76
## 54                 75
## 55                 73
## 56                 65
## 57                 57
## 58                 60
## 59                 53
## 60                 52
## 61                 51
## 62                 51
## 63                 50
## 64                 53
## 65                 52
## 66                 57
## 67                 58
## 68                 59
## 69                 60
## 70                 61
## 71                 65
## 72                 66
## 73                 67
## 74                 66
## 75                 68
## 76                 70
## 77                 68
## 78                 69
## 79                 69
## 80                 71
## 81                 72
## 82                 72
## 83                 77
## 84                 79
## 85                 80
## 86                 82
## 87                 84
## 88                 79
## 89                 99
## 90                100
## 91                100
## 92                100
## 93                100
## 94                100
## 95                100
## 96                100
## 97                100
## 98                100
## 99                100
## 100               100
## 101               100
## 102               100
## 103               100
## 104               100
## 105               100
## 106               100
## 107               100
## 108               100
## 109               100
## 110               100
## 111               100
## 112               100
## 113               100
## 114               100
## 115               100
## 116               100
## 117               100
## 118               100
## 119               100
## 120               100
## 121               100

Practice - R Language

  1. Read and assign your csv data (Header or seperator ?). “18397_Cekmekoy_Omerli_15dk.txt”
  2. Check the class and structure of your new data.
  3. Take the “Temperature” parameter and assign it as a new variable.
  4. Plot the “temperature” vector.
  5. Print minimum temperature and find which element is the minimum in temperature vector.
  6. change the minimum value with NA and Print.
  7. Plot the new “temperature” vector.
  8. Replace these new temperature values with old temperature values located in your data frame.
  9. Write your data frame as a new csv file.

Practice - R Language

  1. Write your data frame as a new csv file.
write.csv(mydata, file = "new_data.csv")

Practice - Create a Function

Practice - Create a Function

Problem: Take a sample belonged to population, and sum

pop <- 1:6                    # This is my population
pop
## [1] 1 2 3 4 5 6
samp <- sample(pop, size = 2) # This is my sample, I choose two var.
samp
## [1] 4 2
sum(samp)
## [1] 6

Practice - Create a Function

I want to create a new function named roll

my_new_function <- function() {

new_variable_1 <-     # number or something
new_variable_2 <-     # number or something
do_this()

}
roll <- function() {
pop <- 1:6 
samp <- sample(pop, size = 2) 
print(samp)
sum(samp)
}
roll()
## [1] 4 3
## [1] 7

Practice - Create a Function

Problem: I want to assign a population spontaneously.

roll_2 <- function() {
pop <- 
samp <- sample(pop, size = 2) 
print(samp)
sum(samp)
}

roll_2() # This will give error. Because pop in undefined.

roll_2 <- function(pop) {
samp <- sample(pop, size = 2) 
print(samp)
sum(samp)
}

roll_2(pop = 1:27)
## [1]  4 17
## [1] 21

Practice - Create a Function

You can add new options. { } and () are important

sum(1:27)
## [1] 378
# Think about these functions
# mean(), print(), plot(), max(), install.packages(), help(), ...

R Programming - Conditions

R Programming - Conditions

  • Comparison Operators
    • equal (==)
    • not equal (!=)
    • greater or equal to (>=)
    • lower or equal to (<=)
  • Logical Operators
    • the and operator (&)
    • the or operator (|)
    • the not operator (!)
  • if (Stand-Alone) Statement
  • else Statement
  • ifelse Statement
TRUE & TRUE  
TRUE & FALSE
TRUE | FALSE 
!TRUE

2 == 3
5 < 6

c(1,4) >= 6

9 != 8

5 < 6 & 9 != 8
score <- 80
exam_no <- 2

score >= 75 | exam_no == 1

score>=75 & score<90 | exam_no==1

if (Stand-Alone) Statement

The if statement executes a chunk of code if and only if a defined condition is TRUE, which looks something like this:

if(TRUE) message("It was true!")
## It was true!
if(FALSE) message("It wasn't true!")
a <- 3
num <- 4

if ( a <= num ) {
a <- a ^ 2
}

a
## [1] 9

if (Stand-Alone) Statement

num <- -1

if ( num < 0 ) {
  print("num is negative.")
  print("Don't worry, I'll fix it.") 
  num <- num * -1
  print("Now num is positive.")
}
## [1] "num is negative."
## [1] "Don't worry, I'll fix it."
## [1] "Now num is positive."
num
## [1] 1

if (Stand-Alone) Statement

if (Stand-Alone) Statement

else - Statement

If you want something different to happen when the condition is FALSE, you can add an else declaration.

if(FALSE)
    {
      message("This won't execute...")
} else
    {
      message("but this will.")
}
## but this will.
a <- 3.5
dec <- 0.5

if (dec <= 0.5) {
  adec <- dec + 1
} else {
  adec <- dec
}

adec
## [1] 1.5

-else - and -else if- Statement

If your situation has more than two mutually exclusive cases, use else and if statements together.

a <- 1 
b <- 1 

if (a > b) { 
  print("A wins!")
} else if (a < b) { 
  print("B wins!")
} else {
  print("Tie.")
}
## [1] "Tie."

Nesting and Stacking Statements

An if statement can be placed in another if statement. In the editor, modify the mynumber example once more as follows:

Nesting and Stacking Statements

Nesting and Stacking Statements

Nesting and Stacking Statements

Nesting and Stacking Statements

Practice - Conditions - TRUE and FALSE

  1. Print this if it is TRUE. “This message will print!”
if (...) {
  print(...)
} 
  1. Go to sleep if it is TRUE, else wake up.
if (...) {
   print("Go to sleep!")
} else {
   print("Wake up!")
}
  1. The variable message to “I execute this when true!” when the condition is TRUE.
if (...) {
  message <- "..."
  print(message)
} else {
  message <- "I execute this when false!"
  print(...)
}

Practice - Conditions - Logical Op

Problem : You are a CAR, and you are going on the road, BUT ;

Car_Stop_Light <- 'orange'
Number_of_Pedestrians <- 2

Remember rules

if (...) {
  print(...);
} else {
  print(...);
}

Practice - Conditions - Logical Op

ANSWER : You are a CAR, and you are going on the road, BUT ;

Car_Stop_Light <- 'orange'
Number_of_Pedestrians <- 2

Remember rules

if (Car_Stop_Light == 'green' & Number_of_Pedestrians ==0) {
  print('Go!');
} else {
  print('STOP');
}
## [1] "STOP"

Practice - Conditions - Logical Op

Problem : You want to enjoy, and let’s say the day is;

day <- "Saturday"

It is okay, you can fun if it is weekend.

if (...) {
  print('Enjoy the weekend!')
} else {
  print('Do some work.')
}

Practice - Conditions - Logical Op

ANSWER : You want to enjoy, and let’s say the day is;

day <- "Saturday"

It is okay, you can fun if it is weekend.

if (day == 'Saturday' | day == 'Sunday') {
  print('Enjoy the weekend!')
} else {
  print('Do some work.')
}
## [1] "Enjoy the weekend!"

Practice - Conditions - Logical Op

Problem : You want to go out and your question is “Should I take an umbrella?”

  • Check to see, if “weather” is “equal” to “cloudy” and, whether there is a “high_chance_of_rain”.
  • If it is both, the code should assign the value of the variable message to be “Take umbrella!”
  • Otherwise, the code should assign the value of the variable message to “No need for umbrella!”
  • Print the message variable after the conditional statement.
  • Based on the condition, what should its value be?

Note : There are two variables in your code, “weather” and “high_chance_of_rain”

Practice - Conditions - Logical Op

# you want to go out and your question is "Should I take an umbrella?"

message <- 'Should I take an umbrella?'

weather <- "cloudy"

high_chance_of_rain <- TRUE

Practice - Conditions - Logical Op

# you want to go out and your question is "Should I take an umbrella?"

message <- 'Should I take an umbrella?'

weather <- "cloudy"

high_chance_of_rain <- TRUE
if (...) {
  message <- 'Take umbrella!'
  print(message)
} else { 
  message <- 'No need for umbrella!'
  print(message)
  }

Practice - Conditions - Logical Op

# you want to go out and your question is "Should I take an umbrella?"

message <- 'Should I take an umbrella?'

weather <- "cloudy"

high_chance_of_rain <- TRUE

if (weather == "cloudy" & high_chance_of_rain == TRUE) {
  message <- 'Take umbrella!'
  print(message)
} else { 
  message <- 'No need for umbrella!'
  print(message)
  }
## [1] "Take umbrella!"

Next Week

Next Week

Conditions and Loops, DataCamp

(Optional) Practice - MidTerm Project